package com.itheima.reggie.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.common.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Web配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    //配置拦截器的拦截路径
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") //拦截所有
                .excludePathPatterns("/backend/**") //排除所有静态资源
                .excludePathPatterns("/front/**") //排除所有静态资源
                .excludePathPatterns("/employee/login") //排除路径
                .excludePathPatterns("/user/login") //排除路径
                .excludePathPatterns("/user/sendMsg"); //排除路径
    }

    //配置消息转换器（扩展，在框架原来的基础上添加消息转换器）
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //TODO 一行代码搞定
        ((MappingJackson2HttpMessageConverter)converters.get(7)).setObjectMapper(new JacksonObjectMapper());

        //添加一个jackson的消息转换器 MappingJackson2HttpMessageConverter
        //MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //messageConverter.setObjectMapper(new JacksonObjectMapper());//设置消息转换器核心类 JacksonObjectMapper
        //将自己的消息转换器放入集合中
        //converters.add(0, messageConverter); // 一定要放在集合最前面
    }
}
